﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using ModelLayer;
using CinemaDbLib;
using System.Windows.Forms;

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "SeatCtrl" in code, svc and config file together.
public class SeatCtrl : ISeatCtrl
{
	private SeatDB seatDB;
	private ScheduleDB scheduleDB;
	private TicketDB ticketDB;

	public SeatCtrl()
	{
		seatDB = new SeatDB();
		scheduleDB = new ScheduleDB();
		ticketDB = new TicketDB();
	}

	public Seat addSeat(Room room, int row, int col)
	{
		Seat seat = new Seat(-1, room, row, col);

		seat.id = seatDB.insertSeat(seat);
		return seat;
	}

	public void removeSeat(int id)
	{
		seatDB.removeSeat(id);
	}

	public void upateSeat(int id, Room room, int row, int col)
	{
		Seat seat = new Seat(id, room, row, col);
		seatDB.updateSeat(seat);
	}

	public Seat getSeat(int id)
	{
		return seatDB.getSeatById(id);
	}

	public Seat[] getSeats()
	{
		return seatDB.getSeats().ToArray();
	}

	public Seat[] getTakenSeats(int scheduleId)
	{
		Schedule schedule = scheduleDB.getScheduleById(scheduleId);

		List<Seat> takenSeats = new List<Seat>();
		List<Ticket> tickets = ticketDB.getTicketsBySchedule(scheduleId);

		foreach (Seat seat in seatDB.getSeatsByRoom(schedule.room.id))
			foreach (Ticket ticket in tickets)
				if (ticket.seat.id == seat.id)
					takenSeats.Add(seat);

		return takenSeats.ToArray();
	}

	public Seat[] getSeatsByRoom(int id)
	{
		return seatDB.getSeatsByRoom(id).ToArray();
	}

	public int getColCountInRow(int roomId, int rowId)
	{
		return seatDB.getColCount(roomId, rowId);
	}

	public int getRowCount(int roomId)
	{
		return seatDB.getRowCount(roomId);
	}
}
